<template>
	<view class="all-main">
		<view class="main">
			<view class="lists">
				<view class="title">
					<view class="lefts">
						<text>基本信息</text>
					</view>
					<view class="rights">
						<text>钱包余额：</text>
						<text class="balance">{{balance}}</text>
					</view>
				</view>
				<view class="agents">
					<view class="lefts">
						<text>渠道名称</text>
					</view>
					<view class="rights" @click="agentShow = true">
						<text class="tags">{{agent_name}}</text>
						<u-icon name="arrow-right" size="16"></u-icon>
					</view>
				</view>
				<view class="types">
					<view class="lefts">
						<text>费用类型</text>
					</view>
					<view class="rights">
						<radio-group @change="radioChange">
							<label class="radio">
								<radio value="1" size="20" :checked="true" />营销费用
							</label>
							<label class="radio">
								<radio value="2" />业务补贴
							</label>
							<view class="others">
								<label class="radio">
									<radio value="3" />其他
								</label>
								<u--input border="bottom" v-model="type_txt"></u--input>
							</view>
						</radio-group>
					</view>
				</view>
				<view class="moneys">
					<view class="lefts">
						<text>出款金额</text>
					</view>
					<view class="rights">
						<u--input placeholder="请输入出款金额" border="bottom" inputAlign="right" v-model="money"></u--input>
					</view>
				</view>
				<view class="remarks">
					<view class="titles">
						<text>备注</text>
					</view>
					<view class="con">
						<u--textarea v-model="meno" placeholder="请输入备注"></u--textarea>
					</view>
				</view>
				<view class="btns" @click="saveData">
					<text>生产账单</text>
				</view>
			</view>
			<u-picker :show="agentShow" :columns="agentList" keyName="name" closeOnClickOverlay
				@close="agentShow = false" @cancel="agentShow = false" @confirm="agentSelect"></u-picker>
			<u-modal :show="subState" title="账单明细" cancelText="取消" showCancelButton @cancel="subState = false"
				@confirm="saveDataFun">
				<view class="slot-content">
					<view class="items">
						<view class="lefts">
							<text>渠道名称</text>
						</view>
						<view class="rights">
							<text>{{agent_name}}</text>
						</view>
					</view>
					<view class="items">
						<view class="lefts">
							<text>费用类型</text>
						</view>
						<view class="rights">
							<text>{{type_text}}</text>
						</view>
					</view>
					<view class="items">
						<view class="lefts">
							<text>出款金额</text>
						</view>
						<view class="rights">
							<text class="moneys">{{money}}</text>
						</view>
					</view>
					<view class="items">
						<view class="lefts">
							<text>备注</text>
						</view>
						<view class="rights">
							<text>{{meno}}</text>
						</view>
					</view>
				</view>
			</u-modal>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				agentShow: false,
				balance: '0.00',
				money: '',
				meno: '',
				type: 1,
				type_text: '',
				type_txt: '',
				agentList: [],
				agent_id: '',
				agent_name: '请选择渠道',
				subState: false,
			};
		},
		onLoad() {
			this.getAgents()
			this._balance()
		},
		methods: {
			// 获取我的直属
			async getAgents() {
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp({
					url: 'bill/getAgents'
				});
				uni.hideLoading()
				if (res.code == 200) {
					this.agentList = [res.data]
				}
			},
			// 获取余额
			async _balance() {
				const {
					data: res
				} = await this.$myHttp({
					url: 'balance/totals'
				});
				if (res.code == 200) {
					this.balance = res.data
				}
			},
			// 选择费用类型
			radioChange(e) {
				this.type = e.detail.value
				if (e.detail.value < 3) {
					this.type_txt = ''
				}
				if (e.detail.value == 1) {
					this.type_text = "营销费用"
				}
				if (e.detail.value == 2) {
					this.type_text = "业务补贴"
				}
				if (e.detail.value == 3) {
					this.type_text = this.type_txt
				}
			},
			// 选择渠道
			agentSelect(e) {
				this.agent_id = e.value[0].id
				this.agent_name = e.value[0].name + "(" + e.value[0].phone + ")"
				this.agentShow = false
			},
			// 生产账单
			saveData() {
				let that = this
				if (this.agent_id <= 0) {
					uni.showToast({
						title: '请选择渠道',
						duration: 2000,
						icon: 'none'
					});
					return false
				}
				if (this.money * 100 <= 0) {
					uni.showToast({
						title: '请输入出款金额',
						duration: 2000,
						icon: 'none'
					});
					return false
				} else {
					if (this.money * 100 > this.balance * 100) {
						uni.showToast({
							title: '余额不足',
							duration: 2000,
							icon: 'none'
						});
						return false
					}
				}
				this.subState = true
			},
			// 生产账单 - API
			async saveDataFun() {
				var that = this
				uni.showLoading({
					title: '提交中'
				});
				const {
					data: res
				} = await this.$myHttp({
					url: 'bill/setBillAdd',
					data: {
						agent_id: this.agent_id,
						money: this.money,
						type: this.type,
						type_txt: this.type_text,
						meno: this.meno
					}
				});
				uni.hideLoading()
				uni.showToast({
					title: res.msg,
					duration: 2000,
					icon: 'none'
				});
				if (res.code == 200) {
					setTimeout(function() {
						uni.navigateBack({
							delta: 1
						})
					}, 1000)
				}
			},
		}
	}
</script>

<style lang="scss">
	.all-main {
		.main {
			padding: 30rpx;

			.lists {
				background-color: #ffffff;
				border-radius: 10rpx;
				padding: 30rpx;

				.title {
					display: flex;
					justify-content: space-between;
					align-items: center;
					padding-bottom: 30rpx;
					border-bottom: 1px solid #f4f4f5;

					.lefts {
						font-size: 32rpx;
						font-weight: bold;
					}

					.rights {
						color: #909399;
						font-size: 28rpx;

						.balance {
							font-size: 32rpx;
							color: #19be6b;
						}
					}
				}

				.agents {
					display: flex;
					justify-content: space-between;
					align-items: center;
					padding: 30rpx 0;
					border-bottom: 1px solid #f4f4f5;

					.rights {
						display: flex;
						align-items: center;

						.tags {
							color: #909399;
							padding-right: 10rpx;
						}
					}
				}

				.types {
					padding: 30rpx 0;
					border-bottom: 1px solid #f4f4f5;
					display: flex;

					.lefts {
						width: 30%;
					}

					.rights {
						width: 70%;

						.radio {
							margin-right: 30rpx;
							font-size: 28rpx;
						}

						.others {
							margin-top: 30rpx;
							display: flex;
							align-items: center;
						}
					}
				}

				.moneys {
					padding: 30rpx 0;
					display: flex;
					justify-content: space-between;
					align-items: center;
					border-bottom: 1px solid #f4f4f5;

					.rights {
						width: 40%;
					}
				}

				.remarks {
					padding: 30rpx 0;

					.titles {
						padding-bottom: 30rpx;
					}
				}

				.btns {
					margin-top: 50rpx;
					background-color: #19be6b;
					color: #ffffff;
					padding: 20rpx;
					text-align: center;
					border-radius: 10rpx;
				}
			}

			.slot-content {
				width: 100%;

				.items {
					display: flex;
					justify-content: space-between;
					padding: 20rpx 0;
					border-top: 1px solid #f4f4f5;
					
					.lefts {
						font-size: 28rpx;
					}
					
					.rights {
						color: #909399;
						font-size: 28rpx;
						
						.moneys {
							font-size: 36rpx;
							color: #fa3534;
						}
					}
				}
			}
		}
	}
</style>